/**
 * LoginController.java
 */
package org.yate.oa.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.yate.oa.model.LoginEntity;
import org.yate.oa.model.MessageEntity;
import org.yate.oa.service.IMessageService;
import org.yate.oa.service.imp.MessageService;
import org.yate.oa.utils.Constants;

import com.alibaba.fastjson.JSONObject;

/**
 * @author ZYY
 * @date 2013年9月18日
 * @version 1.0
 */
@WebServlet(urlPatterns = { "/msg/get", "/msg/post" }, asyncSupported = true)
public class MessageController extends BaseServletController {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		res.setCharacterEncoding("utf-8");
		req.setCharacterEncoding("utf-8");
		res.setContentType("text/html; charset=utf-8");
		String type = req.getParameter("type");
		if ("getall".equals(type)) {
			getAll(req, res);
		} else if ("getnew".equals(type)) {
			getNew(req, res);
		} else if ("delete".equals(type)) {
			deleteBy(req, res);
		}
	}

	protected void getAll(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		LoginEntity le = (LoginEntity) req.getSession().getAttribute(Constants.SESSION_USER_ENTITY);
		IMessageService msgSvr = this.springCtx.getBean(MessageService.class);
		List<MessageEntity> mes = msgSvr.getEntitiesBy(le.getUserId());
		if (mes.size() > 0) {
			json.put("code", "200");
			json.put("data", mes);
		} else {
			json.put("code", "201");
			json.put("msg", "no message");
		}
		out.println(json);
		out.flush();
		out.close();
	}

	protected void getNew(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		LoginEntity le = (LoginEntity) req.getSession().getAttribute(Constants.SESSION_USER_ENTITY);
		IMessageService msgSvr = this.springCtx.getBean(MessageService.class);
		List<MessageEntity> mes = msgSvr.getNewEntitiesBy(le.getUserId());
		if (mes.size() > 0) {
			json.put("code", "200");
			json.put("data", mes);
		} else {
			json.put("code", "201");
			json.put("msg", "no message");
		}
		out.println(json);
		out.flush();
		out.close();
	}

	protected void deleteBy(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		LoginEntity le = (LoginEntity) req.getSession().getAttribute(Constants.SESSION_USER_ENTITY);
		IMessageService msgSvr = this.springCtx.getBean(MessageService.class);
		msgSvr.batchDelete(le.getUserId());
		json.put("code", "200");
		json.put("msg", "delete success");
		out.println(json);
		out.flush();
		out.close();
	}

}
